package com.cenxi.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.cenxi.config.MemberExcelListener;
import com.cenxi.dao.domain.ExcelDomain;
import com.cenxi.dao.domain.Excelc;
import com.cenxi.dao.domain.Excelcx;
import com.cenxi.mapper.ExcelMapper;
import com.cenxi.mapper.ExcelcMapper;
import com.cenxi.mapper.ExxelcxMapper;
import com.cenxi.utils.ExcelUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author cenxi
 * @Date 2023/10/19 15:12
 * @Version 1.0
 */
@RestController
@RequestMapping("/excel")
public class ExcelController {
@Autowired
private ExcelMapper excelMapper;
    @GetMapping("/export")
    public Object export(HttpServletResponse response) {
        long startTime = System.currentTimeMillis();
        ExcelUtils.downLoad(response, ExcelUtils.getExcelDomain(), ExcelDomain.class, ExcelUtils.getFileName("excelLog"));
        long endTime = System.currentTimeMillis();
        long resTime = endTime - startTime;
        System.out.println("成功, 用时: " + resTime);
        return "成功";
    }

    @PostMapping("/exImport")
    public Object exImport (@RequestParam("file") MultipartFile file) throws IOException {
        // 方式一：同步读取，将解析结果返回，比如返回List<Member>，业务再进行相应的数据集中处理
        // 方式二：对照doReadSync()方法的是最后调用doRead()方法，不进行结果返回，而是在MemberExcelListener中进行一条条数据的处理；
        // 此处示例为方式二

        long startTime = System.currentTimeMillis();
        EasyExcelFactory.read(file.getInputStream(), ExcelDomain.class, new MemberExcelListener()).sheet().doRead();
        List<ExcelDomain> list = EasyExcel.read(file.getInputStream())
                .head(ExcelDomain.class)
                .sheet()
                .doReadSync();
        long endTime = System.currentTimeMillis();
        list.forEach(System.out::println);
        long resTime = endTime - startTime;
        System.out.println("成功, 用时: " + resTime);
        return "成功, 用时: " + resTime;
    }
    @Autowired
    private ExcelcMapper excelcMaxper;
    @Autowired
    private ExxelcxMapper excelcxMapper;
    @GetMapping("/save")
    public Object save() {
        List<ExcelDomain> excelDomain = ExcelUtils.getExcelDomain();
        long startTime = System.currentTimeMillis();
        Excelc excelc = new Excelc();
        Excelcx excelcx = new Excelcx();
        for (ExcelDomain domain : excelDomain) {
            excelMapper.insert(domain);
            BeanUtils.copyProperties(domain, excelc);
            excelcMaxper.insert(excelc);
            BeanUtils.copyProperties(domain, excelcx);
            excelcxMapper.insert(excelcx);

        }
//        excelMapper.insertBatch(excelDomain);
        long endTime = System.currentTimeMillis();
        System.out.println("数据添加成功,耗时: " + (endTime - startTime) + "毫秒");
        return "true";
    }


}
